<?php
if(!isset($AppUI->id)||
!isset($_SESSION['user'])||
(isset($_SESSION['user'])&&($_SESSION['user']&1<<2)==0)
) {
	redirect(HOST_BACKEND);
}

// data input
$intUserId	        = $this->input->post('userid', 0);
$strEmail	    	= $this->input->post('email',null);			
$strConfirmEmail    = $this->input->post('email_confirm',null);
$arrErr             = array();

// validate manual
//+ email
if(empty($strEmail)) {
    array_push($arrErr, $arrLocale['email_required']);
}elseif(!CI_Function::isEmail($strEmail) && $strEmail != '') {
    array_push($arrErr, $arrLocale['email_check']);
}
if(!isset($strConfirmEmail) || trim($strConfirmEmail) == '')
    array_push($arrErr, $arrLocale['email_confirm_required']);
//+so sanh email va confirm email
if($strEmail !=$strConfirmEmail)
    array_push($arrErr, $arrLocale['email_notmatch']);
	
if(isset($arrErr) && count($arrErr)==0 ) {//allow save
    // unset session if exists
    if(isset($_SESSION['USER_ERROR'])) unset($_SESSION['USER_ERROR']);
	$this->load->model('user_model', 'user');  
    $intError = $this->user->updateEmail($AppUI->id, $intUserId,$strEmail);
	if($intError==0) CI_Function::setMsg($arrMessage['email_update']);
	elseif($intError==1) array_push($arrErr, $arrError['noperm_change_Perm']); 
	elseif($intError==2) array_push($arrErr, $arrError['noperm_change_range']);  
}
	
	// occur error
	if (isset($arrErr) && count($arrErr) > 0) {
		$_SESSION['USER_ERROR']    = $arrErr;
		redirect(HOST_BACKEND . 'user/changemail?userid='.$intUserId);
	}
redirect(HOST_BACKEND.'user');
